package com.wzt.gift.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * @author Zenith
 */
@Configuration
@EnableWebSecurity(debug = false)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 密码加密器
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 授权规则配置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 屏蔽跨域
        http.csrf().disable()
                // 对请求做授权处理
                .authorizeRequests()
                // 放行登录请求
                .antMatchers("/login").permitAll()
                // 对登录页面跳转路径放行
                .antMatchers("/login.html").permitAll()
                // 拦截其他请求
                .anyRequest().authenticated()
                // 允许表单登录
                .and().formLogin()
                .successForwardUrl("/loginUser/loginSuccess")
                .and().logout().permitAll();
    }

    /**
     * 认证管理器配置
     * @return
     * @throws Exception
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}

